package com.huiquan.section.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.section.service.SectionBlackService;

@Controller
public class SectionBlackController {

	@Autowired
	private SectionBlackService sectionBlackService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/section/section_black_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameKey");
		String nameSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String section = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "section");
		String oldSection = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "oldSection");

		return sectionBlackService.list(start, codeSearch, nameSearch, countPerPageStr,section,oldSection);
	}

	@RequestMapping(value = "/section/section_black_pre_edit.htm")
	public ModelAndView pre_edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		return sectionBlackService.edit(id);
	}

	@RequestMapping(value = "/section/section_black_export.do")
	public void export(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		sectionBlackService.export(resp);
	}

	@ResponseBody
	@RequestMapping(value = "/section/section_black_import.do")
	public ReturnData sectionImport(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required = false) MultipartFile file) throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		ReturnData rtn = null;
		try {
			rtn = sectionBlackService.insertBatch(user, file);
		} catch (RuntimeException e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage() );
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/section/section_black_delete.do")
	public ModelAndView deleteSectionCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");

		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameKey");
		String nameSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String id = req.getParameter("id");
		String section = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "section");
		String oldSection = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "oldSection");

		return sectionBlackService.deleteSectionBlack(id, start, codeSearch, nameSearch, countPerPageStr,section,oldSection);
	}

	@ResponseBody
	@RequestMapping(value = "/section/submit_section_black_edit.do")
	public ReturnData sectionCodeAdd(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("id");
		String diseaseName = req.getParameter("diseaseName");
		String originalSection = req.getParameter("originalSection");
		String correctSection = req.getParameter("correctSection");
		User user = (User) req.getSession().getAttribute("user");
		String changeFlag = req.getParameter("changeFlag");
		ReturnData rtn = null;
		try {
			rtn = sectionBlackService.sectionBlackAdd(id, diseaseName, originalSection, correctSection, changeFlag,user);
		} catch (RuntimeException e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage() );
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}
}
